<?
    class ListingImageManager {
        static function GetImages($listingid,$comments = false) {
            $dir = Conf::GetImageDir();
            if (is_numeric($listingid))
                $dbimages = DBM::FetchAllRows("SELECT * FROM listing_images WHERE listingid=$listingid ORDER BY upload_date ASC;");
            else {
                $dbimages = array();
                ErrorMessage::Show("No Such Listing","Could not load photos for listing with ID: $listingid");
            }
            
            $ret = array();
            foreach ($dbimages as $image) {
                if (is_file("$dir/$image[imagename]")) {
                    if ($comments)
                        $ret["$dir/$image[imagename]"] = $image['comment'];
                    else
                        $ret[$image['imagename']] = "$dir/$image[imagename]";
                }
            }
            
            return $ret;
        }
        static function GetCommentFor($listingid,$image) {
            $allcomments = self::GetImages($listingid,true);
            return $allcomments[$image];
        }
        static function GetImage($listingid,$index) {
            $all = self::getImages($listingid);
            return $all[$index];
        }
        static function GetImageByID($imageid) {
            $query = "SELECT * FROM listing_images WHERE id=$imageid;";
            $parts = DBM::FetchRow($query);
            $filename = Conf::GetImageDir() . "/" . $parts['imagename'];
            
            return $filename;
        }
        static function GetAThumbnail($listingid) {
            $images = self::GetImages($listingid);
            return self::getThumbnail(array_shift($images));
        }

        static function CountImages($listingid) {
            return DBM::CountRows("SELECT * FROM listing_images WHERE  listingid=$listingid;");
        }
        
        static function HasImages($listingid) {
            return (self::CountImages($listingid) > 0);
        }
        static function ThumbnailOf($image) {
            return self::getThumbnail($image);
        }
        
        private static function getThumbnail($image) {
            if (is_file($image)) {
                $width = SettingManager::GetSetting("listing_images_thumbnail_width");
                $height = SettingManager::GetSetting("listing_images_thumbnail_height");
                
                // keep proportions -- figure out which side is the biggest, then scale to that instead
                // of scaling to a fixed size.
                list($x,$y) = getimagesize($image);
                if ($x > $y)  $height = null;
                else if ($y > $x) $width = null;
                
                $thumb = new Thumbnail($image,$width,$height,PATH);
                $path = $thumb->Render();
                return $path;
            }
            else return ThemeManager::GetImage("nophotos");
        }
        static function AsThumbnail($image) {
            return self::getThumbnail($image); 
        }
        static function GetThumbnails($listingid,$comments) {
            $images = self::GetImages($listingid,$comments);
            $thumbs = array();
            
			if ($comments) {
				foreach ($images as $img => $comment) {
					$thumbs[self::getThumbnail($img)] = $comment;
				}
			}
			else {
	            foreach ($images as $img) {
	                $thumbs[] = self::getThumbnail($img);
	            }
			}
			return $thumbs;
        }
    }
?>
